package mmc.objects;

import static org.lwjgl.opengl.GL11.glPopMatrix;
import static org.lwjgl.opengl.GL11.glPushMatrix;
import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glScalef;
import static org.lwjgl.opengl.GL11.glTexCoord2f;
import static org.lwjgl.opengl.GL11.glTranslatef;

import org.lwjgl.opengl.GL11;

import mmc.Client;
import mmc.Constants;
import mmc.EngineObject;
import mmc.MovableEngineObject;
import mmc.Player;
import mmc.resource.Texture;
import mmc.resource.TextureLoader;

@SuppressWarnings("serial")
public class RemotePlayer extends MovableEngineObject{
	public float pitch, yaw;
	public String name;
	public double currentPing;
	public int target = -1;
	static Texture body;
	static Texture head;

	static
	{
		if(!Constants.IS_SERVER){
			body = TextureLoader.load("mmc/resource/data/braun.png");
			head = TextureLoader.load("mmc/resource/data/gold.png");
		}
	}
	
	public RemotePlayer(mmc.server.Player p){
		super(p.id,p.x,p.y,p.z,p.vx,p.vy,p.vz);
		this.name = p.getName();
		health = p.health;
		energy = p.energy;
		stats = p.stats;
		this.currentPing = p.currentPing;
		this.pitch = p.pitch;
		this.yaw = p.yaw;
	}

	@Override
	public void render(EngineObject observer) {		
		Player p = Client.client.getScene().getPlayer();
		if(p.getRemotePlayer()==this){
			return; //don't draw yourself
			//glPushMatrix();
			//glTranslatef(p.x,p.y,p.z);
		}else{
			glPushMatrix();
			glTranslatef(x,y,z);
		}
		
		glRotatef(-yaw, 0.0f, 1.0f, 0.0f);
		
		
	
		
		body.bind();
		
		glScalef(Player.THICKNES*3/2, Player.SIZE*2/5 ,Player.THICKNES);
		glTranslatef(0,Player.SIZE*4/5,0);
		drawBox();				//draw body
		
		glScalef(0.25f, 1f, 0.25f);
		glTranslatef(-Player.THICKNES*2,-Player.SIZE/2,0);
		drawBox();
		
		glTranslatef(Player.THICKNES*4,0,0);
		drawBox();
		
		head.bind();
		glTranslatef(Player.THICKNES*5.5f, Player.SIZE/2,0);
		drawBox();
		
		glTranslatef(-Player.THICKNES*15,0,0);
		drawBox();
		
		
		glScalef(1.5f, 0.3f, 2f);
		glTranslatef(Player.THICKNES*5, Player.SIZE*4/3,0);
		drawBox();
		
/*		glScalef(0.1f, 1f, 0.5f);
		glTranslatef(0,-Player.SIZE,0);
		drawBox();
		
		head.bind();
		glScalef(0.2f, 0.3f, 0.5f);
		glTranslatef(0,4,0);		
		drawBox();				//draw head		
	*/	
		
		glPopMatrix();
	}
	
	public void drawBox(){
		GL11.glBegin(GL11.GL_QUADS); // Start Drawing Quads
		// Front Face
		GL11.glNormal3f(0.0f, 0.0f, 0.5f); // Normal Pointing Towards Viewer
		glTexCoord2f(0, 0);
		GL11.glVertex3f(-0.5f, -0.5f, 0.5f); // Point 1 (Front)
		glTexCoord2f(1, 0);
		GL11.glVertex3f(0.5f, -0.5f, 0.5f); // Point 2 (Front)
		glTexCoord2f(1, 1);
		GL11.glVertex3f(0.5f, 0.5f, 0.5f); // Point 3 (Front)
		glTexCoord2f(0, 1);
		GL11.glVertex3f(-0.5f, 0.5f, 0.5f); // Point 4 (Front)
		// Back Face
		GL11.glNormal3f(0.0f, 0.0f, -0.5f); // Normal Pointing Away From Viewer
		glTexCoord2f(0, 0);
		GL11.glVertex3f(-0.5f, -0.5f, -0.5f); // Point 1 (Back)
		glTexCoord2f(1, 0);
		GL11.glVertex3f(-0.5f, 0.5f, -0.5f); // Point 2 (Back)
		glTexCoord2f(1, 1);
		GL11.glVertex3f(0.5f, 0.5f, -0.5f); // Point 3 (Back)
		glTexCoord2f(0, 1);
		GL11.glVertex3f(0.5f, -0.5f, -0.5f); // Point 4 (Back)
		// Top Face
		GL11.glNormal3f(0.0f, 0.5f, 0.0f); // Normal Pointing Up
		glTexCoord2f(0, 0);
		GL11.glVertex3f(-0.5f, 0.5f, -0.5f); // Point 1 (Top)
		glTexCoord2f(1, 0);
		GL11.glVertex3f(-0.5f, 0.5f, 0.5f); // Point 2 (Top)
		glTexCoord2f(1, 1);
		GL11.glVertex3f(0.5f, 0.5f, 0.5f); // Point 3 (Top)
		glTexCoord2f(0, 1);
		GL11.glVertex3f(0.5f, 0.5f, -0.5f); // Point 4 (Top)
		// Bottom Face
		GL11.glNormal3f(0.0f, -0.5f, 0.0f); // Normal Pointing Down
		glTexCoord2f(0, 0);
		GL11.glVertex3f(-0.5f, -0.5f, -0.5f); // Point 1 (Bottom)
		glTexCoord2f(1, 0);
		GL11.glVertex3f(0.5f, -0.5f, -0.5f); // Point 2 (Bottom)
		glTexCoord2f(1, 1);
		GL11.glVertex3f(0.5f, -0.5f, 0.5f); // Point 3 (Bottom)
		glTexCoord2f(0, 1);
		GL11.glVertex3f(-0.5f, -0.5f, 0.5f); // Point 4 (Bottom)
		// Right face
		GL11.glNormal3f(0.5f, 0.0f, 0.0f); // Normal Pointing Right
		glTexCoord2f(0, 0);
		GL11.glVertex3f(0.5f, -0.5f, -0.5f); // Point 1 (Right)
		glTexCoord2f(1, 0);
		GL11.glVertex3f(0.5f, 0.5f, -0.5f); // Point 2 (Right)
		glTexCoord2f(1, 1);
		GL11.glVertex3f(0.5f, 0.5f, 0.5f); // Point 3 (Right)
		glTexCoord2f(0, 1);
		GL11.glVertex3f(0.5f, -0.5f, 0.5f); // Point 4 (Right)
		// Left Face
		GL11.glNormal3f(-0.5f, 0.0f, 0.0f); // Normal Pointing Left
		glTexCoord2f(0, 0);
		GL11.glVertex3f(-0.5f, -0.5f, -0.5f); // Point 1 (Left)
		glTexCoord2f(1, 0);
		GL11.glVertex3f(-0.5f, -0.5f, 0.5f); // Point 2 (Left)
		glTexCoord2f(1, 1);
		GL11.glVertex3f(-0.5f, 0.5f, 0.5f); // Point 3 (Left)
		glTexCoord2f(0, 1);
		GL11.glVertex3f(-0.5f, 0.5f, -0.5f); // Point 4 (Left)
		GL11.glEnd(); // Done Drawing Quads
	}

	@Override
	public void update(float dt) {
		if(vy!=0)
			vy -= 30*dt;
			
		x += vx*dt;
		y += vy*dt;
		z += vz*dt;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public boolean copyTo(EngineObject o){
		RemotePlayer p = (RemotePlayer) o;
		//
		copyFieldsTo(p);
		p.pitch = pitch;
		p.yaw = yaw;
		p.currentPing = currentPing;
		
		if(Client.client.getScene().player.getRemotePlayer() == o){
			Client.client.getScene().player.health = p.health;
			Client.client.getScene().player.energy = p.energy;
			Client.client.getScene().player.stats = p.stats;
		}
		
		return true;
	}
}
